package cn.hp.service.impl;

import cn.hp.bean.Customer;
import cn.hp.bean.CustomerExample;
import cn.hp.bean.Order;
import cn.hp.bean.OrderExample;
import cn.hp.mapper.CustomerMapper;
import cn.hp.mapper.OrderMapper;
import cn.hp.service.OrderService;
import cn.hp.utils.JsonData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public JsonData addOrder(Order order) {
        int result = orderMapper.insertSelective(order);
        if (result >=0){
            return JsonData.buildSuc("添加成功");
        }
        return JsonData.buildError("添加失败");
    }

    @Override
    public JsonData selectListByPhone(String phone) {
        OrderExample orderExample=new OrderExample();//新建条件查询类
        //example.setOrderByClause("id asc"); //按照id排序
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andPhoneEqualTo(phone); // where and phoen = #{phone}
        //criteria.andCreatetimeEqualTo("2020-11-11");
        List<Order> orders = orderMapper.selectByExample(orderExample);
        return JsonData.buildSuc(orders);
    }

    @Override
    public JsonData selectOrdersList(Map<String, Object> map, Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
        List<Order> list=orderMapper.selectOrdersList(map);
        if(list.size()>0){
            PageInfo<Order> pageInfo=new PageInfo<Order>(list);
            return JsonData.buildSuc(pageInfo);
        }
        return JsonData.buildError("数据有误");
    }

    @Override
    public JsonData updateSta(String id, String status) {
        Order order=new Order();
        order.setId(Integer.valueOf(id));
        order.setStatus(status);

        int result = orderMapper.updateByPrimaryKeySelective(order);
        if (result >=0){
            return JsonData.buildSuc("修改状态成功");
        }
        return JsonData.buildError("修改状态失败");
    }

    @Override
    public int updateOrderStatus(Integer id) {
        Order order=new Order();
        order.setStatus("1");
        OrderExample orderExample=new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = orderMapper.updateByExampleSelective(order, orderExample);
        return i;
    }

}
